﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Memery.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    12/03/2012 20:12 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using IveelySE.Common;
using IveelySE.Common.Log;
using IveelySE.Config;
using IveelySE.Liquidate;
using IveelySE.NLP.Index;
using IveelySE.NLP.Common.DataStructure;
using IveelySE.NLP.Recognition;

#endregion

namespace IveelySE.Index
{
    /// <summary>
    ///   索引的内存存储
    /// </summary>
    public class Memery
    {
        #region 属性或字段

        /// <summary>
        ///   全文索引
        /// </summary>
        private static Invert _invertedIndex = new Invert();

        /// <summary>
        ///  知识索引
        /// </summary>
        private static Invert _EventIndex = new Invert();

        /// <summary>
        /// 知识集合
        /// </summary>
        private static Hashtable _Event = new Hashtable();

        #endregion

        #region 公有方法

        /// <summary>
        ///   加载到内存
        /// </summary>
        public static void Load()
        {
            _invertedIndex = (Invert)Serialize.UnSerailzable(Application.GetIndexDir());
            _EventIndex = (Invert)Serialize.UnSerailzable(Application.GetEventIndex());
            _Event = (Hashtable)Serialize.UnSerailzable(Application.GetEventFile());
            StopWords.Load();
            Shield.Load();
        }

        /// <summary>
        ///   写入文件
        /// </summary>
        /// <param name="invertedIndex"> </param>
        public static void Create(Invert invertedIndex, string fileName)
        {
            Serialize.Serailzable(invertedIndex, fileName);
        }

        /// <summary>
        ///   搜索查找文档集
        /// </summary>
        /// <param name="keywords"> </param>
        /// <returns> 返回文档集 </returns>
        public static IList<double> GetDocumentByKeywords(string[] keywords)
        {
            //如果不包含非法字符
            if (!Shield.Process(keywords) && _invertedIndex != null)
            {
                return _invertedIndex.FindCommonDocumentByKeys(keywords);
            }
            return null;
        }

        public static IList<double> GetKnowledgeBywords(string[] keywords)
        {
            //如果不包含非法字符
            if (!Shield.Process(keywords) && _EventIndex != null)
            {
                return _EventIndex.FindCommonDocumentByKeys(keywords);
            }
            return null;
        }

        public static Information GetKnowledgeByIds(IList<double> ids)
        {
            int realId = int.Parse(ids[0].ToString(CultureInfo.InvariantCulture));
            return (Information) _Event[realId];

            ////TODO:时间复杂度太高
            //List<string> result = new List<string>();

            //foreach (double id in ids)
            //{
            //    int realId = int.Parse(id.ToString());//.ToString();
            //    try
            //    {
            //        result.Add(((Information)_Event[realId]).FormatString());
            //    }
            //    catch (Exception exception)
            //    {
            //        Common.Log.Content.GetInstance().Write(exception, MessageType.Warning);
            //    }

            //}


            //return result;
        }

        #endregion
    }
}